package com.copa.module.goods.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.copa.domain.ResponseDto;
import com.copa.module.cart.service.GoodsManage;
import com.copa.module.goods.domain.dto.*;
import com.copa.module.goods.domain.entity.GoodsEntity;
import com.copa.module.goods.mapper.GoodsDao;
import com.copa.module.goods.service.GoodsService;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author copa
 * @createDate 2021-01-14 12:03
 * @function
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;

    @Autowired
    private GoodsManage goodsManage;

    @Override
    public ResponseDto<List<IndexGoodsListDto>> indexList() {
        List<IndexGoodsListDto> goodsListDtos = goodsDao.findIndexList();
        if (ObjectUtil.isNotEmpty(goodsListDtos)
                && ObjectUtil.length(goodsListDtos) > 0) {
            return ResponseDto.success(goodsListDtos);
        }
        return ResponseDto.errorMsg("没有查找商品！");
    }

    @Override
    public ResponseDto<List<IndexGoodsListDto>> categoryGoods(Long categoryId) {
        List<IndexGoodsListDto> goodsListDtos = goodsDao.findGoodsByCategoryId(categoryId);
        if (ObjectUtil.isNotEmpty(goodsListDtos)
                && ObjectUtil.length(goodsListDtos) > 0) {
            return ResponseDto.success(goodsListDtos);
        }
        return ResponseDto.errorMsg("没有查找商品！");
    }

    @Override
    public ResponseDto<GoodsInfoDetailDto> detail(Long goodsId) {
        GoodsEntity goodsEntity = goodsDao.selectById(goodsId);
        if (goodsEntity == null || goodsEntity.getIsOnSale() == 0) {
            return ResponseDto.errorMsg("该商品不存在！");
        }

        ResponseDto<List<GoodsProductDto>> goodsProductDtoRes = goodsManage.findGoodsSpuByGoodsId(goodsId);
        if (goodsProductDtoRes.isNotSuccess()) {
            return ResponseDto.errorMsg(goodsProductDtoRes.getMsg());
        }

        ResponseDto<List<GoodsAttributeDto>> goodsAttributeDtoRes=
                goodsManage.findGoodsAttributeByGoodsId(goodsId);
        if (goodsAttributeDtoRes.isNotSuccess()) {
            return ResponseDto.errorMsg(goodsAttributeDtoRes.getMsg());
        }

        ResponseDto<List<GoodsSpecDto>> goodsSpecDtoRes=
                goodsManage.findGoodsSpecByGoodsId(goodsId);
        if (goodsSpecDtoRes.isNotSuccess()) {
            return ResponseDto.errorMsg(goodsSpecDtoRes.getMsg());
        }

        // 整理
        GoodsInfoDetailDto goodsInfoDetailDto = new GoodsInfoDetailDto();
        GoodsDetailDto goodsDetailDto = new GoodsDetailDto();
        BeanUtils.copyProperties(goodsEntity, goodsDetailDto);
        goodsInfoDetailDto.setGoodsDetailDto(goodsDetailDto)
                .setGoodsProductDto(goodsProductDtoRes.getData())
                .setGoodsAttributeDto(goodsAttributeDtoRes.getData())
                .setGoodsSpecDto(goodsSpecDtoRes.getData());
        return ResponseDto.success(goodsInfoDetailDto);
    }

    @Override
    public ResponseDto<List<IndexGoodsListDto>> search(String goodsName) {
        List<IndexGoodsListDto> goodsListDtos = Lists.newLinkedList();
        if (ObjectUtil.isNotNull(goodsName) && goodsName.trim().length() > 0) {
            goodsListDtos = goodsDao.findGoodsByGoodsName(goodsName);
        }
        if (ObjectUtil.isNotEmpty(goodsListDtos)
                && ObjectUtil.length(goodsListDtos) > 0) {
            return ResponseDto.success(goodsListDtos);
        }
        return ResponseDto.errorMsg("没有查找商品！");
    }
}
